package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.MyMovement;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加好友
    @Override
    public void addContacts(Long userId, Long friendId) {
        // 1.你加他
        // 1-1 判断是否曾经添加过此人好友
        boolean exists1 = mongoTemplate.exists(new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);
        if (exists1 == false) {
            // 1-2实现添加好友
            Friend myTofriend = new Friend();
            myTofriend.setUserId(userId); // 你的id
            myTofriend.setFriendId(friendId); // 好友的id
            myTofriend.setCreated(System.currentTimeMillis()); // 添加时间
            mongoTemplate.save(myTofriend);
            // 1-3 查询好友（对方）的个人动态   movement_mine_2
            List<MyMovement> firendMyMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friendId);
            // 1-4 将他的动态设置到我的好友动态中  movement_friend_99
            if (CollectionUtil.isNotEmpty(firendMyMovementList)) {
                for (MyMovement firendMyMovement : firendMyMovementList) {
                    // 创建我的好友动态数据
                    FriendMovement myFriendMovement = new FriendMovement();
                    myFriendMovement.setUserId(friendId); // 好友的id
                    myFriendMovement.setPublishId(firendMyMovement.getPublishId()); // 动态id
                    myFriendMovement.setCreated(firendMyMovement.getCreated()); // 发布时间
                    mongoTemplate.save(myFriendMovement, ConstantUtil.MOVEMENT_FRIEND + userId);
                }
            }
        }

        // 2.他加你
        // 2-1 判断是否曾经添加过此人好友
        boolean exists2 = mongoTemplate.exists(new Query(Criteria.where("friendId").is(userId).and("userId").is(friendId)), Friend.class);
        if (exists2 == false) {
            // 2-2实现添加好友
            Friend friendToMy = new Friend();
            friendToMy.setUserId(friendId); // 好友的id
            friendToMy.setFriendId(userId); // 你的id
            friendToMy.setCreated(System.currentTimeMillis()); // 添加时间
            mongoTemplate.save(friendToMy);
            // 2-3 查询我的个人动态 movement_mine_99
            List<MyMovement> myMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);
            // 2-4 将我的个人动态设置到好友的好友动态表  movement_friend_2
            if (CollectionUtil.isNotEmpty(myMovementList)) {
                for (MyMovement myMovement : myMovementList) {
                    // 创建好友的好友动态表
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setUserId(userId); // 你的id
                    friendMovement.setPublishId(myMovement.getPublishId()); // 你的动态id
                    friendMovement.setCreated(myMovement.getCreated()); // 动态发布时间
                    mongoTemplate.save(friendMovement,ConstantUtil.MOVEMENT_FRIEND+friendId);
                }
            }
        }
    }


    // 好友列表
    @Override
    public PageBeanVo findFriendByPage(Long userId, Integer pageNum, Integer pageSize) {
        // 1.构建条件
        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).skip((pageNum-1)*pageSize).limit(pageSize);
        // 2.查询list
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        // 3.查询总记录数
        long count = mongoTemplate.count(new Query(
                Criteria.where("userId").is(userId)
        ), Friend.class);
        // 4.返回并封装分页对象
        return new PageBeanVo(pageNum, pageSize, count, friendList);
    }

    //粉丝喜欢
    @Override
    public void fanLove(UserLike userLike) {
        //调用方法保存
        mongoTemplate.save(userLike);
    }

    //粉丝不喜欢
    @Override
    public void fanDisLove(Long userId, Long likeUserId) {

        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("likeUserId").is(likeUserId)
        );
        mongoTemplate.remove(query, UserLike.class);
    }

    //删除好友
    @Override
    public void deleteContacts(Long userId, Long friendId) {
        //构建条件
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("friendId").is(friendId)
        );
        ///删除好友，根据自己的id将结果从数据库表中移除
        mongoTemplate.remove(query, Friend.class);
    }

}
